package devicedbgui;

import devicedbgui.data.TestCaseDetails;
import devicedbgui.data.TestResultDAO;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

/** Displays test case details */
public class TestCaseDetailsPanel extends javax.swing.JPanel {
    Hashtable<Integer, TestCaseDetails> htTestCaseDetails = new Hashtable<Integer, TestCaseDetails>();
    /** Reference to GUI class for accessing its variables */ 
    GUI gui;
     String testCase;
    //** Constructor creates all components belonging to panel */   
    public TestCaseDetailsPanel(GUI gui, String testCase) {
        initComponents();
        this.gui = gui;
        this.testCase = testCase;
        loadData(gui);
    }
    /** Displays error messages */  
    public void showError(JFrame parent, String error)
    {
        JOptionPane.showMessageDialog(parent, error);
    }
    /** Displays test result stats from database */
    private void loadData(GUI gui)
    {
        TestResultDAO dd = new TestResultDAO(gui.connection);
        try {
            ArrayList<TestCaseDetails> records = dd.getTestCaseDetails(testCase);
            DefaultTableModel model = (DefaultTableModel)tblTestCaseDetails.getModel();
            model.getDataVector().clear();
            int index = 0;   
            htTestCaseDetails.clear();
            for (TestCaseDetails item : records){ 
                htTestCaseDetails.put(new Integer(index++), item);
                model.addRow(new Object[]{item.statement, item.numberOfFailed, item.numberOfPassed});
            }
        }
        catch (Exception ex){
            showError(gui.frame, ex.toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblTestCaseDetails = new javax.swing.JTable(){    public Component prepareRenderer(TableCellRenderer renderer,
            int rowIndex, int vColIndex) {
            Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
            if (rowIndex % 2 == 0 && !tblTestCaseDetails.isRowSelected(rowIndex)) {
                c.setBackground(new Color(237, 242, 251));
            }
            else if (tblTestCaseDetails.isRowSelected(rowIndex)){c.setBackground(new Color(184, 207, 229));}
            else { c.setBackground(getBackground());}
            return c;
        }
    }
    ;

    tblTestCaseDetails.setAutoCreateRowSorter(true);
    tblTestCaseDetails.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null}
        },
        new String [] {
            "Statement", "Number of Times Statement Failed", "Number of Times Statement Passed"
        }
    ) {
        boolean[] canEdit = new boolean [] {
            false, false, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    tblTestCaseDetails.setGridColor(new java.awt.Color(204, 204, 255));
    tblTestCaseDetails.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jScrollPane1.setViewportView(tblTestCaseDetails);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
            .addContainerGap())
    );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTestCaseDetails;
    // End of variables declaration//GEN-END:variables

}
